home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / package_16-january-2001.zip / Effects / cheapo fixer.cpp < prev    next >
C/C++ Source or Header  |  2000-10-14  |  6KB  |  258 lines

  1. // Copyright (C) Mikko Apo (apo@iki.fi)
  2. // The following code may be used to write free software
  3. // if credit is given to the original author.
  4. // Using it for anything else is not allowed without permission
  5. // from the author.
  6.  
  7. /*
  8.  
  9.   Random number generator from Oskari Tammelin's Noise.cpp Noise generator
  10.   FSM did the logarithmic volume slider mathematic formula
  11.  
  12. */
  13.  
  14. /*
  15. Revision history:
  16.   1.03  Fixed a stupid bug. This caused the fixer to fix
  17.         only first half of a stereo signal block.
  18.         Cheapo fixer pro makes this machine obsolete.
  19.   1.02  Lowered the default noise level to -168.4dB
  20.   1.01    Optimized Tick()
  21.   1.0    First release.
  22.   */
  23.  
  24. #include <stdlib.h>
  25. #include <time.h>
  26. #include <math.h>
  27. #include <string.h>
  28. #include "../mdk.h"
  29.  
  30. #define COMMAND_STRING "About..."
  31. #define MACHINE_NAME "cheapo fixer"
  32. #define SHORT_NAME "ch.fixer"
  33. #define MACHINE_AUTHOR "Mikko Apo (apo@iki.fi)"
  34. #define MAX_TRACKS        0
  35. #define MIN_TRACKS        0
  36. #define NUMGLOBALPARAMETERS 2
  37. #define NUMTRACKPARAMETERS 0
  38. #define NUMATTRIBUTES 0
  39. #define MI__VERSION__ "1.03"
  40.  
  41. //    Parameters
  42.  
  43. CMachineParameter const paraThreshold = 
  44. { pt_word, "Threshold","Threshold [0 - 32768]",0,32768,0xffff,MPF_STATE,0 };
  45.  
  46. CMachineParameter const paraLevel = 
  47. { pt_byte, "Noise Level","Whitenoise Level",0,0x7f,0xff,MPF_STATE,0x20 };
  48.  
  49. // List of all parameters, track parameters last
  50.  
  51. CMachineParameter const *pParameters[] = 
  52. { ¶Threshold,¶Level };
  53.  
  54. #pragma pack(1)
  55.  
  56. class gvals
  57. {
  58. public:
  59.     word threshold;
  60.     byte level;
  61. };
  62.  
  63. #pragma pack()
  64.  
  65. // Machine's info
  66.  
  67. CMachineInfo const MacInfo = 
  68. {
  69.     MT_EFFECT,MI_VERSION,MIF_DOES_INPUT_MIXING,MIN_TRACKS,MAX_TRACKS,
  70.     NUMGLOBALPARAMETERS,NUMTRACKPARAMETERS,pParameters,NUMATTRIBUTES,NULL,
  71. #ifdef _DEBUG
  72.     MACHINE_NAME" [DEBUG]"
  73. #else
  74.     MACHINE_NAME
  75. #endif
  76.     ,SHORT_NAME,MACHINE_AUTHOR,COMMAND_STRING
  77. };
  78.  
  79.  
  80. class miex : public CMDKMachineInterfaceEx
  81. {
  82.  
  83. };
  84.  
  85. class mi : public CMDKMachineInterface
  86. {
  87. public:
  88.     mi();
  89.  
  90.     virtual void Command(int const i);
  91.     virtual void Tick();
  92.     virtual char const *DescribeValue(int const param, int const value);
  93.  
  94.     virtual void MDKInit(CMachineDataInput * const pi);
  95.     virtual bool MDKWork(float *psamples, int numsamples, int const mode);
  96.     virtual bool MDKWorkStereo(float *psamples, int numsamples, int const mode);
  97.     virtual void MDKSave(CMachineDataOutput * const po) { }
  98.  
  99.     public:
  100.     virtual CMDKMachineInterfaceEx *GetEx() { return &ex; }
  101.     virtual void OutputModeChanged(bool stereo) {};
  102.  
  103.     public:
  104.     miex ex;
  105.     gvals gval;
  106.  
  107. private:
  108.  
  109.     float valThreshold;
  110.     int RandStat;
  111.     float amp;
  112.     int valLevel;
  113. };
  114.  
  115.  
  116. DLL_EXPORTS
  117.  
  118. mi::mi()
  119. {
  120.     GlobalVals = &gval;
  121. }
  122.  
  123. void mi::Command(int const i)
  124. {
  125.     static char txt[500];
  126.     switch(i)
  127.     {
  128.     case 0:
  129.         pCB->MessageBox(MACHINE_NAME"\n\nBuild date: "__DATE__"\nVersion: "MI__VERSION__"\nCoded by: "MACHINE_AUTHOR"\nThanks to Oskari and FSM for code snippets.\n\nCheck out http://www.iki.fi/apo/buzz/\nfor more buzz stuff.\n\nExcellent skin made by Hymax.");
  130.         break;
  131.  
  132. /*    case 1:
  133.         {
  134.             unsigned int c;
  135.             int stat=0x16BA2118;
  136.             int smallest=0x7fffffff, biggest=0xffffffff;
  137.             int null_counter=0;
  138.             for(c=1;c<=0xffff;c++)
  139.             {
  140.                 stat = ((stat * 1103515245 + 12345) & 0xffff) - 0x8000;
  141.                 if(stat>biggest) biggest=stat;
  142.                 if(stat<smallest) smallest=stat;
  143.                 if(!stat) null_counter++;
  144.             }
  145.             sprintf(txt,"Random test:\nSmallest: 0x%x (%ld)\nBiggest: 0x%x (%ld)\nNull counter: %ld\n",smallest,smallest,biggest,biggest,null_counter);
  146.             pCB->MessageBox(txt);
  147.         }
  148. */    }
  149. }
  150.  
  151. char const *mi::DescribeValue(int const param, int const value)
  152. {
  153.     static char txt[100];
  154.  
  155.     switch(param)
  156.     {
  157.     case 0:
  158.         if(value)
  159.         {
  160.           sprintf(txt,"%d",value);
  161.         } else
  162.         {
  163.             return("0.5");
  164.         }
  165.         break;
  166.     case 1:
  167.             sprintf(txt,"%.2fdB",20*(log10((((valThreshold)?valThreshold:0.5)*pow(2.0,(1-value/127.0)*(-16))/32768))));
  168.         break;
  169.     }
  170.     return txt;
  171. }
  172.  
  173. void mi::MDKInit(CMachineDataInput * const pi)
  174. {
  175.     valThreshold = (float)paraThreshold.DefValue;
  176.     valLevel = paraLevel.DefValue;
  177.     RandStat = 0x16BA2118;    // initialize random generator
  178.     amp=(float)((pow(2.0,(1-valLevel/127.0)*(-16))*valThreshold)/0x7fff);
  179. }
  180.  
  181. void mi::Tick()
  182. {
  183.     bool changed=false;
  184.     if (gval.threshold != paraThreshold.NoValue)
  185.     {
  186.         valThreshold=gval.threshold;
  187.         changed=true;
  188.     }
  189.     if (gval.level != paraLevel.NoValue)
  190.     {
  191.         valLevel=gval.level;
  192.         changed=true;
  193.     }
  194.     if(changed)
  195.     {
  196.     if(valThreshold)
  197.     {
  198.         amp = (float) ((pow(2.0,(1-valLevel/127.0)*(-16))*valThreshold)/0x7fff);
  199.     } else
  200.     {
  201.         amp = (float) ((pow(2.0,(1-valLevel/127.0)*(-16))*0.5)/0x7fff);
  202.     }
  203.     }
  204.  
  205.  
  206. bool mi::MDKWorkStereo(float *psamples, int numsamples, int const mode)
  207. {
  208.     if ((mode==WM_WRITE)||(mode==WM_NOIO))
  209.     {
  210.         return false;
  211.     }
  212.  
  213.     if (mode == WM_READ)        // <thru>
  214.         return true;
  215.  
  216.     do 
  217.     {
  218.         if(fabs(*psamples)<=valThreshold)
  219.         {
  220.             RandStat = ((RandStat * 1103515245 + 12345) & 0xffff) - 0x8000;
  221.             *psamples= amp*RandStat;
  222.         }
  223.         psamples++;
  224.         if(fabs(*psamples)<=valThreshold)
  225.         {
  226.             RandStat = ((RandStat * 1103515245 + 12345) & 0xffff) - 0x8000;
  227.             *psamples= amp*RandStat;
  228.         }
  229.         psamples++;
  230.     } while(--numsamples);
  231.  
  232.     return true;
  233. }
  234.  
  235. bool mi::MDKWork(float *psamples, int numsamples, int const mode)
  236. {
  237.     if ((mode==WM_WRITE)||(mode==WM_NOIO))
  238.     {
  239.         return false;
  240.     }
  241.  
  242.     if (mode == WM_READ)        // <thru>
  243.         return true;
  244.  
  245.     do 
  246.     {
  247.         if(fabs(*psamples)<=valThreshold)
  248.         {
  249.             RandStat = ((RandStat * 1103515245 + 12345) & 0xffff) - 0x8000;
  250.             *psamples=amp*RandStat;
  251.         }
  252.         psamples++;
  253.     } while(--numsamples);
  254.  
  255.     return true;
  256. }
  257.